package gen;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class GenerateImage {

    public static void main(String[] args) {
        for (int x = 0; x < 100; x++) {
            generateImage(x);
        }
    }

    public static void generateImage(int i) {
        int width = 132;
        int height = 132;

        // 创建一张空白的图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // 获取画笔
        Graphics2D g2d = image.createGraphics();

        try {
            // 设置背景色
            g2d.setColor(Color.CYAN); // 淡蓝色
            g2d.fillRect(0, 0, width, height);

            // 设置文字颜色和字体
            g2d.setColor(Color.BLACK);
            g2d.setFont(new Font("Arial", Font.BOLD, 20));

            // 写文字
            String text = "lottery" + i;
            // 让文字居中
            FontMetrics fm = g2d.getFontMetrics();
            int textWidth = fm.stringWidth(text);
            int textHeight = fm.getAscent();
            int x = (width - textWidth) / 2;
            int y = (height + textHeight) / 2 - 5;
            g2d.drawString(text, x, y);

            // 保存图片到本地
            File outputfile = new File("target/Lottery" + i + ".png");
            ImageIO.write(image, "png", outputfile);

            System.out.println("图片生成成功，保存路径：" + outputfile.getAbsolutePath());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            g2d.dispose(); // 释放资源
        }
    }
}
